//
//  FKHomeCouponPopView.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/20.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKHomeCouponPopView.h"
#import "FKHomeCouponCell.h"

@interface FKHomeCouponPopView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UIImageView *couponBgView;
@property (nonatomic, weak) UITableView *tableView;

@end

@implementation FKHomeCouponPopView

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIView *container = [[UIView alloc] init];
    [self addSubview:container];
    self.container = container;
    container.frame = CGRectMake(0, fkScreenH, fkScreenW, fkScreenH);
    
    UIImageView *couponBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coupon_pop_bg"]];
    [container addSubview:couponBgView];
    self.couponBgView = couponBgView;
    couponBgView.userInteractionEnabled = true;
    CGFloat couponBgH = fk_adjustW(392);
    CGFloat couponBgY = (fkScreenH - couponBgH) * 0.5 - fk_adjustW(40);
    couponBgView.frame = CGRectMake(0, couponBgY, fkScreenW, couponBgH);
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [couponBgView addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIButton *getBtn = [UIButton fk_btnWithTarget:self action:@selector(getBtnClicked) img:[UIImage imageNamed:@"coupon_get"]];
    [container addSubview:getBtn];

    UIImageView *bottomCoverView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coupon_bottom_cover"]];
    [couponBgView addSubview:bottomCoverView];
    
    [getBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(couponBgView.mas_bottom);
        make.centerX.offset(0);
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(fk_adjustW(130));
        make.top.offset(fk_adjustW(114));
        make.bottom.offset(fk_adjustW(-35));
        make.width.offset(fk_adjustW(268));
        make.centerX.offset(0);
    }];
    [bottomCoverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(50));
    }];
}

- (void)getBtnClicked
{
    [SVProgressHUD showSuccessWithStatus:@"领取成功"];
    [self dismiss];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKHomeCouponCell *cell = [FKHomeCouponCell cellWith:tableView];
    cell.data = self.datas[indexPath.section];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(86);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return fk_adjustW(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.container.fk_top = 0;
//        self.couponBgView.fk_centerY = fkScreenH * 0.5 - fk_adjustW(40);
    } completion:^(BOOL finished) {
        self.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.25 animations:^{
//        self.couponBgView.fk_top = fkScreenH;
        self.container.alpha = 0;
        self.backgroundColor = [UIColor clearColor];
//        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
